package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.MenuItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YYY on 2016/4/30.
 */
public class AddUserDialogMenuAdapter extends RecyclerView.Adapter<AddUserDialogMenuVH> implements View.OnClickListener {

    private Context context;
    private List<MenuItem> menuItems;
    private LayoutInflater inflater;
    private List<Boolean> selectStates;

    public AddUserDialogMenuAdapter(Context context, List<MenuItem> menuItems) {
        this.context = context;
        this.menuItems = menuItems;
        initStates();
        inflater = LayoutInflater.from(context);
    }


    @Override
    public AddUserDialogMenuVH onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = inflater.inflate(R.layout.dialog_add_user_menu_item_layout, null);
        return new AddUserDialogMenuVH(itemView);
    }

    @Override
    public void onBindViewHolder(AddUserDialogMenuVH holder, int position) {

        MenuItem menuItem = menuItems.get(position);

        if (selectStates.get(position))
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_checked);
        }else {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_uncheck);
        }

        holder.menuNameView.setText(menuItem.getLabel());
        holder.itemView.setTag(R.id.recyclerview_item_position, position);
        holder.itemView.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {
        if (null == menuItems) {
            return 0;
        } else {
            return menuItems.size();
        }

    }

    private void initStates() {
        if (getItemCount() > 0) {
            selectStates = new ArrayList<>();
            for (int i = 0; i < getItemCount(); i++) {
                selectStates.add(false);
            }
        }
    }

    public void setSelectStates(List<Boolean> selectStates) {
        this.selectStates = selectStates;
        initStates();
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_menu_item:
                int position = (int) v.getTag(R.id.recyclerview_item_position);
                selectStates.set(position, selectStates.get(position) ? false : true);
                notifyDataSetChanged();
                break;
        }
    }

    public String getSelectMenus()
    {
        StringBuffer menus=new StringBuffer();
        for (int i=0;i<getItemCount();i++)
        {
            if (selectStates.get(i))
            {
                menus.append(menuItems.get(i).getMenuId()).append(",");
            }

        }
        return menus.toString();
    }

    public void setHasSelctedMenus(String navList)
    {

        if (null!=navList)
        {
            String[] menus=navList.split(",");
            if (null!=menus&&menuItems!=null)
            {
                for (String menuid:menus)
                {
                    for (int i=0;i<getItemCount();i++)
                    {
                        if (menuid.equals(String.valueOf(menuItems.get(i).getMenuId())))
                        {
                            selectStates.set(i,true);
                        }
                    }
                }
            }
        }

    }
}
